package com.cy.login.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.comment.util.JwtUtils;
import com.cy.login.comment.exection.ServiceException;
import com.cy.login.domain.Account;
import lombok.extern.slf4j.Slf4j;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cy.login.mapper.ServerMapper;
import com.cy.login.domain.Server;
import com.cy.login.service.ServerService;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.annotation.Resource;


/**
 * 服务器列表(Server)表服务实现类
 *
 * @author cxy
 * @since 2024-07-11 15:11:47
 */
@Slf4j
@Service
public class ServerServiceImpl extends ServiceImpl<ServerMapper, Server> implements ServerService {
    @Resource
    private ValueOperations<Object, Object> valueOperations;
    @Resource
    private ServerMapper serverMapper;
    
    /////////////////////////////////////////////////////////////////////////////////////////////////////// 业务部分
    @Override
    public List<Server> proLogListJson(String token) {
        List<Server> list = this.list();
        list.forEach(server -> {
            String key = "server:" + server.getId();
            valueOperations.set(key, server, 1, TimeUnit.DAYS);
            server.setServerKey(null);
        });
        return list;
    }

    @Override
    public String enterServer(String token,String serverUrl) {
        String userNameFormJwt = JwtUtils.getUserNameFormJwt(token);
        Account account = (Account) valueOperations.get(userNameFormJwt);
        if (account == null) {
            throw new ServiceException("token已失效");
        }
        LambdaQueryWrapper<Server> serverLambdaQueryWrapper = new LambdaQueryWrapper<>();
        serverLambdaQueryWrapper.eq(Server::getUrl, serverUrl);
        Server server = serverMapper.selectOne(serverLambdaQueryWrapper);
        if (server == null) {
            throw new ServiceException("服务器不存在");
        }
        return JwtUtils.createJwtToken(account.getUid() +","+server.getServerKey());
    }
    /////////////////////////////////////////////////////////////////////////////////////////////////////// crud+缓存部分
    @Transactional
    public Server create(String redisKey, Server create) {
        serverMapper.insert(create);
        Server server = serverMapper.selectById(create);
        valueOperations.set(redisKey, server,1, TimeUnit.DAYS);
        return server;
    }
    
    @Transactional
    public void delete(String redisKey, Server delete) {
        Server select = this.select(redisKey,delete);
        serverMapper.deleteById(select);
        valueOperations.getOperations().delete(redisKey);
    }
  
    @Transactional
    public Server update(String redisKey, Server update) {//更新存在元素(redis+mysql)
        LambdaQueryWrapper<Server> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(update.getId()!=null,Server::getId, update.getId());
        // 这里添加搜索条件
        Server select = serverMapper.selectOne(wrapper);
        update.setId(select.getId());
        serverMapper.updateById(update);
        valueOperations.set(redisKey, update, 1, TimeUnit.DAYS);
        return update;
    }
    
    
    public Server select(String redisKey,Server select) {
        Server server = (Server) valueOperations.get(redisKey);
        if (server != null){return server;}
        LambdaQueryWrapper<Server> wrapper = new LambdaQueryWrapper<>();
        // 添加查询条件
        
        // 查询
        server = serverMapper.selectOne(wrapper);
        return server;
    }
    
}
